/*
 * TermOccurrenceSequentialFile_gtest.h
 *
 *  Created on: Nov 27, 2012
 *      Author: bruno
 */

/*
 * TermSequentialFile_gtest.h
 *
 *  Created on: Nov 27, 2012
 *      Author: bruno
 */

#include <gtest/gtest.h>
#include <memory>
#include <vector>
#include "../physical_layer/file_system/TermOccurrence.h"
#include "../physical_layer/file_system/TermOccurrenceSequentialFile.h"

using std::vector;
using physical::TermOccurrence;
using physical::TermOccurrenceSequentialFile;

/**
 * Test Suite de la clase TermSequentialFile
 */
class TermOccurrenceSequentialFileTestFixture: public ::testing::Test {

public:

	std::string m_filename;

	TermOccurrenceSequentialFileTestFixture() {
		const ::testing::TestInfo* const test_info =
				::testing::UnitTest::GetInstance()->current_test_info();

		std::string filename(test_info->name());
		filename.append(".");
		filename.append(test_info->test_case_name());
		filename.append(".tmp");

		m_filename = filename;
	}

	virtual ~TermOccurrenceSequentialFileTestFixture() {
	}

	void SetUp() {
	}

	void TearDown() {
	}

	const char* getFileNameForTest() {
		return m_filename.c_str();
	}
};

/**
 * Test Add and retrieve one term occurrence in the dictionary
 *
 *
 */TEST_F( TermOccurrenceSequentialFileTestFixture, PutAndGetTermOccurrence) {

	TermOccurrenceSequentialFile termsOccurrenceFile;

	termsOccurrenceFile.open(getFileNameForTest(),
			std::ios_base::in | std::ios_base::out | std::ios_base::trunc);

	TermOccurrence term(1, 1);

	termsOccurrenceFile.put(term);

	termsOccurrenceFile.close();

	termsOccurrenceFile.open(getFileNameForTest(),
				std::ios_base::in | std::ios_base::out);

	TermOccurrence retrievedTerm = termsOccurrenceFile.get();

	ASSERT_TRUE(retrievedTerm == term);
}

/**
 * Test hasNext term occurrence of the term occurrence sequential file
 *
 *
 */TEST_F( TermOccurrenceSequentialFileTestFixture, HasNextTermOccurrence) {

	TermOccurrenceSequentialFile termsOccurrenceFile;

	termsOccurrenceFile.open(getFileNameForTest(),
			std::ios_base::in | std::ios_base::out | std::ios_base::trunc);

	TermOccurrence term1(1,1);

	ASSERT_FALSE(termsOccurrenceFile.hasNext());

	termsOccurrenceFile.put(term1);

	ASSERT_TRUE(termsOccurrenceFile.hasNext());

	termsOccurrenceFile.close();

	termsOccurrenceFile.open(getFileNameForTest(),
				std::ios_base::in | std::ios_base::out);

	ASSERT_TRUE(termsOccurrenceFile.hasNext());
}

/**
 * Test Add and retrieve several term occurrences of the term occurrence sequential file
 *
 *
 */TEST_F( TermOccurrenceSequentialFileTestFixture, PutAndGetSeveralTermOccurrences) {

	 TermOccurrenceSequentialFile termsOccurrenceFile;

	termsOccurrenceFile.open(getFileNameForTest(),
			std::ios_base::in | std::ios_base::out | std::ios_base::trunc);

	TermOccurrence term1(1,1);

	termsOccurrenceFile.put(term1);

	ASSERT_TRUE(termsOccurrenceFile.hasNext());

	TermOccurrence term2(2,2);

	termsOccurrenceFile.put(term2);

	TermOccurrence term3(3,3);

	termsOccurrenceFile.put(term3);

	termsOccurrenceFile.close();

	termsOccurrenceFile.open(getFileNameForTest(),
				std::ios_base::in | std::ios_base::out);

	TermOccurrence retrievedTerm = termsOccurrenceFile.get();

	ASSERT_TRUE(retrievedTerm == term1);

	ASSERT_TRUE(termsOccurrenceFile.hasNext());

	retrievedTerm = termsOccurrenceFile.get();

	ASSERT_TRUE(retrievedTerm == term2);

	retrievedTerm = termsOccurrenceFile.get();

	ASSERT_TRUE(retrievedTerm == term3);
}


